package com.compty.paint.manager;

import java.io.Serializable;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.compty.core.hibernate.HibernateEntityDao;
import com.compty.core.page.Page;
import com.compty.paint.domain.Auction;
import com.compty.paint.domain.AuditState;
import com.compty.paint.domain.ImageInfo;
import com.compty.paint.domain.UploadState;
import com.compty.paint.util.Constants;

public class AuctionManager extends  HibernateEntityDao<Auction>{
	
	private UploadStateManager uploadStateManager;
	private AuditStateManager auditStateManager;
	private ImageInfoManager imageInfoManager;
	

	public void setUploadStateManager(UploadStateManager uploadStateManager) {
		this.uploadStateManager = uploadStateManager;
	}
	public void setAuditStateManager(AuditStateManager auditStateManager) {
		this.auditStateManager = auditStateManager;
	}
	 public void setImageInfoManager(ImageInfoManager imageInfoManager) {
		this.imageInfoManager = imageInfoManager;
	}
	/**
	 *@Todo:分页查询
	 *@CreateTime:2011-8-18 上午11:31:21
	 * @param curPage 当前页码
	 * @param pageSize  显示条数
	 */
	public Page findByPage(int curPage, int pageSize){
		
		Criteria criteria = createCriteria();
		criteria.addOrder(Order.desc("id"));
		
        UploadState  uploadState = uploadStateManager.findUniqueBy("english_name", Constants.Upload_State_Init); //上传状态正常
        criteria = criteria.add(Restrictions.eq("uploadState", uploadState)); 
        AuditState auditState = auditStateManager.findUniqueBy("english_name", Constants.Audit_State_Pass);  //审核状态通过
        criteria = criteria.add(Restrictions.eq("auditState", auditState)); 

		return super.pagedQuery(criteria, curPage,pageSize);
	}
	
	/**
	 *@Todo:由acutionId查找画册图片集
	 *@author:zhuqh
	 *@CreateTime:2011-8-21 上午08:36:55
	 * @param auctionId
	 */
	public Page findImageInfosByAuctionId(int pageNo,int pageSize,Serializable auctionId){
		
		String hql = "select p from Auction a inner join a.imagesInfo p  where a.id=? order by p.id asc";
		
		return pagedQuery(hql, pageNo, pageSize, auctionId);
		
	}
	
	/**
	 *@Todo:删除画册图片
	 *@CreateTime:2011-8-30 上午09:51:50
	 * @param imageId  图片ID
	 * @param auctionId  拍卖预展ID
	 */
	public void deleteImage(Long imageId, Long auctionId) {
		// TODO Auto-generated method stub
		Auction auction  = get(auctionId);
		ImageInfo imageInfo = imageInfoManager.get(imageId);
		auction.getImagesInfo().remove(imageInfo);
		imageInfoManager.removeById(imageId);
		super.save(auction);
	}

}
